//
//  TBRMineSettingC.m
//  TBRentClient
//
//  Created by tonbright on 2018/4/27.
//  Copyright © 2018年 tonbright. All rights reserved.
//

#import "TBRLoginInSettingC.h"
#import "TBRMineSettingIconCell.h"
#import "TBRMineSettingBasicCell.h"
#import "TBRMineModel.h"
#import <YYModel.h>
#import <UIImageView+WebCache.h>
#import "PhotoBaseManager.h"
#import "TBRLoginController.h"

@interface TBRLoginInSettingC ()<UITableViewDelegate, UITableViewDataSource>
@property (nonatomic, strong) UITableView      *tableView;
@property (nonatomic,strong)NSMutableArray *dataArray;
@property (nonatomic,strong)NSMutableArray *localPhotoArray;//图片数组-------
@property (nonatomic,strong)NSMutableArray *urlPhotoArray;//图片链接数组-------------
@end

@implementation TBRLoginInSettingC

- (void)viewDidLoad {
    [super viewDidLoad];
    [self setupUI];
    
    //加载数据
    [self loadData];
}

- (void)setupUI {
    //设置导航条
    [self setupNavItem];
    
    //添加tableView
    [self setupTableView];
}

- (void)setupNavItem {
    self.titleText = @"个人设置";
    [self ISG_NavigationBarRightBarWithTitle:@"跳过" andAction:@selector(skipBtnClick)];
}

- (void)setupTableView {
    self.tableView = [[UITableView alloc] initWithFrame:CGRectZero style:UITableViewStylePlain];
    [self.view addSubview:self.tableView];
    self.tableView.backgroundColor = [UIColor colorWithHex:0xe6e6e6];
    [self.tableView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.leading.trailing.top.bottom.offset(0);
    }];
    self.tableView.delegate = self;
    self.tableView.dataSource = self;
    self.tableView.tableFooterView = [UIView new];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRMineSettingIconCell" bundle:nil] forCellReuseIdentifier:@"TBRMineSettingIconCellId"];
    [self.tableView registerNib:[UINib nibWithNibName:@"TBRMineSettingBasicCell" bundle:nil] forCellReuseIdentifier:@"TBRMineSettingBasicCellId"];
    if ([self.tableView respondsToSelector:@selector(setSeparatorInset:)]) {
        [self.tableView setSeparatorInset:UIEdgeInsetsMake(0, -45, 0, 0)];
    }
    self.tableView.showsVerticalScrollIndicator = NO;
}

// MARK: - 跳过按钮点击事件
- (void)skipBtnClick {
    //跳转到登录界面--------------
    for (UIViewController *controller in self.navigationController.viewControllers) {
        if ([controller isKindOfClass:[TBRLoginController class]]) {
            TBRLoginController *loginInC =(TBRLoginController *)controller;
            [self.navigationController popToViewController:loginInC animated:YES];
        }
    }
}

#pragma mark - 返回组
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
    return self.dataArray.count;
}

#pragma mark - 返回行
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    NSArray *tmpArray = self.dataArray[section];
    return tmpArray.count;
}

#pragma mark - 返回每行内容
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    //自定义cell
    UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:[self getIdentifier:indexPath] forIndexPath:indexPath];
    [self setupCell:cell indexPath:indexPath];
    return cell;
}

- (NSString *)getIdentifier:(NSIndexPath *)indexPath {
    NSString *iden = @"TBRMineSettingIconCellId";
    if (indexPath.section == 0 && indexPath.row == 0) {
        iden = @"TBRMineSettingIconCellId";
    }else {
        iden = @"TBRMineSettingBasicCellId";
    }
    return iden;
}

- (void)setupCell:(UITableViewCell *)cell indexPath:(NSIndexPath *)indexPath {
    if (indexPath.section == 0 && indexPath.row == 0) {
        TBRMineSettingIconCell *iconCell = (TBRMineSettingIconCell *)cell;
        //取出本地图片数组和网络图片数组
        if (self.localPhotoArray.count != 0) {
            NSData *imageData = self.localPhotoArray.firstObject;
            iconCell.imv_icon.image = [UIImage imageWithData:imageData];
        }else if (self.urlPhotoArray.count != 0) {
            NSURL *urlStr = [NSURL URLWithString:self.urlPhotoArray.firstObject];
            //            [cell.myImageView sd_setImageWithURL:urlStr placeholderImage:[UIImage imageNamed:@"工资"]];
            [iconCell.imv_icon sd_setImageWithURL:urlStr];
        }else {
            //设置为默认图片
            [iconCell.imv_icon setImage:[UIImage imageNamed:@"头像"]];
        }
    }else {
        TBRMineSettingBasicCell *infoBasicCell = (TBRMineSettingBasicCell *)cell;
        NSArray *tmpArray = self.dataArray[indexPath.section];
        TBRMineInfoBasicModel *basicModel = tmpArray[indexPath.row];
        
        infoBasicCell.basicModel = basicModel;
        //        infoBasicCell.basicModel = basicModel;
    }
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return indexPath.section == 0 && indexPath.row == 0 ? 75 : 44;
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 0 && indexPath.row == 0) {
        //弹出是否拍照-------
        if (self.urlPhotoArray.count == 0 && self.localPhotoArray.count == 0) {
            [self showAlert];
        }else {
            //所有照片
            NSMutableArray *allPhoto = [NSMutableArray array];
            //添加已上传照片
            for (NSString *photoUrl in self.urlPhotoArray) {
                UIImage *image = [[SDImageCache sharedImageCache] imageFromDiskCacheForKey:photoUrl];
                if (image) {
                    [allPhoto addObject:UIImageJPEGRepresentation(image, 1)];
                } else {
                    //                    [allPhoto addObject:UIImageJPEGRepresentation([UIImage imageNamed:@"logo-1-0"], 1)];
                }
                
            }
            //添加本地拍摄照片
            for (NSData *imageData in self.localPhotoArray) {
                [allPhoto addObject:imageData];
            }
            [[PhotoBaseManager photoBaseShareManage] tapBrowser:0 imageMutableArray:allPhoto viewController:self];
            __weak typeof(self) weakSelf = self;
            [PhotoBaseManager photoBaseShareManage].deletPicture = ^(NSInteger deleteNum){
                //删除照片
                if (weakSelf.urlPhotoArray.count != 0) {
                    //删除已有照片
                    [weakSelf.urlPhotoArray removeObjectAtIndex:deleteNum];
                } else {
                    //删除拍摄的照片
                    [self.localPhotoArray removeObjectAtIndex:deleteNum];
                }
                
                [weakSelf.tableView reloadData];
                
            };
        }
    }else if (indexPath.section == 2) {
    }else {
        
    }
}


// MARK: - 加载数据
- (void)loadData {
    [self loadLocalData];
    [self loadNetData];
}
// MARK: - 本地数据
- (void)loadLocalData {
    self.dataArray = [NSMutableArray array];
    self.localPhotoArray = [NSMutableArray array];
    self.urlPhotoArray = [NSMutableArray array];
    NSArray *tmpArray = @[
                          @[@{@"messageId":@"userName",@"imageName":@"",@"titleName":@"用户名",
                              @"detailName":@"丁晓龙"},@{@"messageId":@"userName",@"imageName":@"",@"titleName":@"用户名",@"detailName":@"丁晓龙"}],
                          @[@{@"messageId":@"phoneNum",@"imageName":@"手机",@"titleName":@"手机",@"detailName":@"13373633086"}],
                          @[@{@"messageId":@"password",@"imageName":@"",@"titleName":@"登录密码",@"detailName":@"未设置"},
                            @{@"messageId":@"payType",@"imageName":@"",@"titleName":@"支付方式",@"detailName":@"预付卡扣款"},]
                          ];
    
    for (NSArray *tmpEnumArray in tmpArray) {
        NSArray *tmpModelArray = [NSArray yy_modelArrayWithClass:[TBRMineInfoBasicModel class] json:tmpEnumArray];
        [self.dataArray addObject:tmpModelArray];
    }
    
}

// MARK: - 获取网络数据-----
- (void)loadNetData {
    
}



- (void)showAlert {
    //1.弹出窗口
    UIAlertController *alertController = [UIAlertController alertControllerWithTitle:@"温馨提示" message:@"请选择类型" preferredStyle:UIAlertControllerStyleAlert];
    //     UIAlertControllerStyleActionSheet
    //1.1添加确定动作
    UIAlertAction *caramaAction = [UIAlertAction actionWithTitle:@"相机" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //点击后进行的操作----跳转到相机界面
        
        //根据有无图片来判断是拍照还是大图查看
        __weak typeof(self) weakSelf = self;
        [[PhotoBaseManager photoBaseShareManage] getPhotoPicture:self compress:YES photoSize:100 photoWidth:500 edited:NO photoBlock:^(NSData *imageData) {
            //            [model.localPhotos addObject:imageData];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            [weakSelf.localPhotoArray addObject:imageData];
            [weakSelf.tableView reloadData];
        }];
        
        
        
    }];
    UIAlertAction *photoAction = [UIAlertAction actionWithTitle:@"相册" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        //从相册获取
        __weak typeof(self) weakSelf = self;
        [[PhotoBaseManager photoBaseShareManage] getPhotoPictureFromAlbum:self compress:YES photoSize:100 photoWidth:500 edited:NO photoBlock:^(NSData *imageData) {
            //            [model.localPhotos addObject:imageData];
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            [weakSelf.localPhotoArray addObject:imageData];
            [weakSelf.tableView reloadData];
        }];
        
        //根据有无图片来判断是拍照还是大图查看  大图查看
        [[PhotoBaseManager photoBaseShareManage] getPhotoPicture:self compress:YES photoSize:100 photoWidth:500 edited:NO photoBlock:^(NSData *imageData) {
            [weakSelf dismissViewControllerAnimated:YES completion:nil];
            [weakSelf.localPhotoArray addObject:imageData];
            [weakSelf.tableView reloadData];
        }];
    }];
    [alertController addAction:caramaAction];
    [alertController addAction:photoAction];
    //1.2添加取消动作
    UIAlertAction *cancelAction = [UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil];
    [alertController addAction:cancelAction];
    
    [self presentViewController:alertController animated:YES completion:nil];
}
@end
